package ${packageName}.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import ${packageName}.dto.RespResultDto;
import ${packageName}.entity.${tableInfo.tableClassName};
import ${packageName}.entity.${tableInfo.tableClassName}Example;
import ${packageName}.service.${tableInfo.tableClassName}Service;
import com.tongjiuniversity.base.util.DateTimeUtils;
import ${packageName}.utils.Menu;

/**
 * ${tableInfo.tableRemark} 控制器
 **/
@Controller
@RequestMapping("/${tableInfo.objectBeanName}")
public class ${tableInfo.tableClassName}Controller extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	private ${tableInfo.tableClassName}Service ${tableInfo.objectBeanName}Service;
	

	/**
	 * 主页
	 * @return
	 */
	@Menu
    @RequestMapping(value = "/list")
    public String index() {
        return "${tableInfo.objectBeanName}/list";
    }

    /**
     * 分页
     * @param request
     * @param response
     * @param page
     * @param rows
     */
    @RequestMapping(value="/pagelist")
    @ResponseBody
    public String pagelist(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,${tableInfo.tableClassName} ${tableInfo.objectBeanName}){

        String jsonString = StringUtils.EMPTY;
        String jsondata = StringUtils.EMPTY;

        //分页查询
        PageHelper.startPage(page, rows);
        ${tableInfo.tableClassName}Example example=new ${tableInfo.tableClassName}Example();
        ${tableInfo.tableClassName}Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("create_time desc");

        Page<${tableInfo.tableClassName}> list = ${tableInfo.objectBeanName}Service.selectByExamplePage(example);
        
        if (list != null && list.size() > 0) {
            jsonString = JSON.toJSONString(list);
        }

        jsondata = "{\"page\":\""
                + page + "\"," +"\"total\":"
                + list.getPages() + "," + "\"records\":\""
                + list.getTotal() + "\"," + "\"rows\":"
                + jsonString + "}";

        return jsondata;
    }

    /**
     * 跳转编辑页面
     */

    @RequestMapping(value="/editJump")
    @ResponseBody
    public ${tableInfo.tableClassName} editJump(HttpServletRequest request, HttpServletResponse response,${tableInfo.tablePkInfo.javaType} ${tableInfo.tablePkInfo.javaName}) {
        return ${tableInfo.objectBeanName}Service.selectByPrimaryKey(${tableInfo.tablePkInfo.javaName});
    }
    
    /**
	 * 删除 *
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
    public RespResultDto delete(ModelMap map, @RequestParam(required = false) List<${tableInfo.tablePkInfo.javaType}> ${tableInfo.tablePkInfo.javaName}s) {
		RespResultDto vo=new RespResultDto();
        vo.setResult(false);
        
        if(${tableInfo.objectBeanName}Service.delete(${tableInfo.tablePkInfo.javaName}s)){
            vo.setResult(true);
        }
       
        return vo;
	}

    @RequestMapping(value = "/saveModel")
    public String saveModel(HttpServletRequest request, HttpServletResponse response,@RequestParam(required = false) String action, ${tableInfo.tableClassName} ${tableInfo.objectBeanName}) {

        try {
         
            if (action.equals("add")) {
                ${tableInfo.objectBeanName}.setCreateTime(DateTimeUtils.getCurrentDate());
                ${tableInfo.objectBeanName}.setCreatePerson(getUserName(request, response).getLoginName());
                ${tableInfo.objectBeanName}Service.insert(${tableInfo.objectBeanName});
            } else {
                ${tableInfo.objectBeanName}.setLastUpdatePerson(getUserName(request, response).getLoginName());
                ${tableInfo.objectBeanName}.setLastUpdateTime(DateTimeUtils.getCurrentDate());
                ${tableInfo.objectBeanName}Service.updateByPrimaryKeySelective(${tableInfo.objectBeanName});
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/${tableInfo.objectBeanName}/list";
    }


	/**
	 * 下拉列表数据
	 * @return
	 */
	@RequestMapping("/combox")
    @ResponseBody
    public List<${tableInfo.tableClassName}> findAllSysFunction() {
        ${tableInfo.tableClassName}Example example=new ${tableInfo.tableClassName}Example();
        List<${tableInfo.tableClassName}> list = ${tableInfo.objectBeanName}Service.selectByExampleList(example);
        return list;
    }

	/**
	 * 判断对象是否存在
	 * @param 
	 * @return
	 */
    @ResponseBody
    @RequestMapping(value = "/exits")
    public RespResultDto exits(String loginName) {
        RespResultDto vo=new RespResultDto();
        vo.setResult(false);
        try {
            ${tableInfo.tableClassName}Example example=new ${tableInfo.tableClassName}Example();
            List<${tableInfo.tableClassName}> list = ${tableInfo.objectBeanName}Service.selectByExampleList(example);
            if(CollectionUtils.isEmpty(list)){
                vo.setResult(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

}
